<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Gestion des fournisseurs
 * Méthode CRUD (Create, Read, Update, Delete)
 */
class Matiere_premiere extends CI_Controller {

    /**
     * Constructeur de la classe
     * Appel de la librairie
     */
    function __construct() {
        parent::__construct();
        $this->load->model('matiere_premiere_model');
        $this->load->model('stock_model');
        $this->load->model('unite_model');
    }

    function index() {
        $alerts = $this->matiere_premiere_model->getAlerts();

        $matiere_premieres = $this->matiere_premiere_model->getList();
        foreach ($matiere_premieres as $matiere_premiere) {
            $matiere_premiere->STOCK = $this->stock_model->get($matiere_premiere->ARTICLE_ID)->STOCK;
            $matiere_premiere->UNITE_CODE = $this->unite_model->get($matiere_premiere->UNITE_ID)->UNITE_CODE;
        }
        $data = array(
            'matiere_premieres' => $matiere_premieres,
            'alerts' => $alerts
        );

        $this->layout
                ->title('Matières premières')
                ->js('functions')
                ->js('controllers/matiere_premiere')
                ->view('matiere_premiere/index', $data);
    }

    public function table() {


        $matiere_premieres = $this->matiere_premiere_model->getList();
        foreach ($matiere_premieres as $matiere_premiere) {
            $matiere_premiere->STOCK = $this->stock_model->get($matiere_premiere->ARTICLE_ID)->STOCK;
            $matiere_premiere->UNITE_CODE = $this->unite_model->get($matiere_premiere->UNITE_ID)->UNITE_CODE;
        }
        return json_encode($matiere_premieres);
    }

    /*     * *************************************************************************
     * EDIT
     * ************************************************************************ */

    // Create
    public function create() {
        $data['unites'] = $this->unite_model->getList();
        $this->load->view('matiere_premiere/edit', $data);
    }

    public function createDB() {
        $ARTICLE_REF = $this->input->get('ARTICLE_REF');
        $ARTICLE_LIBELLE = $this->input->get('ARTICLE_LIBELLE');
        $ARTICLE_SEUIL = $this->input->get('ARTICLE_SEUIL');
        $UNITE_ID = $this->input->get('UNITE_ID');
        $result = $this->matiere_premiere_model->create($ARTICLE_REF, $ARTICLE_LIBELLE, $ARTICLE_SEUIL, $UNITE_ID);
        echo $this->table();
    }

    // Update
    public function update() {
        $ARTICLE_ID = $this->input->get('ARTICLE_ID');
        $UNITE_ID = $this->unite_model->getList();
        $matiere_premieres = $this->matiere_premiere_model->get($ARTICLE_ID);

        $data = array(
            'matiere_premiere' => $matiere_premieres,
            'unites' => $UNITE_ID
        );

        $this->load->view('matiere_premiere/edit', $data);
    }

    public function updateDB() {
        $ARTICLE_ID = $this->input->get('ARTICLE_ID');
        $ARTICLE_REF = $this->input->get('ARTICLE_REF');
        $ARTICLE_LIBELLE = $this->input->get('ARTICLE_LIBELLE');
        $ARTICLE_SEUIL = $this->input->get('ARTICLE_SEUIL');
        $UNITE_ID = $this->input->get('UNITE_ID');
        $result = $this->matiere_premiere_model->update($ARTICLE_ID, $ARTICLE_REF, $ARTICLE_LIBELLE, $ARTICLE_SEUIL, $UNITE_ID);
        echo $this->table();
    }

    public function Delete() {
        $ARTICLE_ID = $this->input->get('ARTICLE_ID');
        $result = $this->matiere_premiere_model->delete($ARTICLE_ID);
        echo $this->table();
    }

    /*     * *************************************************************************
     * AutoComplete
     * ************************************************************************ */

    public function GetList_Autotcomplete() {
        $maxRows = $this->input->get('maxRows');
        $name_startsWith = $this->input->get('name_startsWith');
        $matieres_premieres = $this->matiere_premiere_model->getList_Autocomplete($maxRows, $name_startsWith);

        $Result = array();
        foreach ($matieres_premieres as $key => $matiere_premiere) {
            $Result[] = array(
                'ID' => $matiere_premiere->ARTICLE_ID,
                'Label' => $matiere_premiere->ARTICLE_REF . ' : ' . $matiere_premiere->ARTICLE_LIBELLE,
                'Value' => $matiere_premiere->ARTICLE_LIBELLE
            );
        }

        echo json_encode($Result);
    }

    public function fournir($idArticle) {
        $fournisseur = $this->input->post('fournisseur');
        if ($fournisseur != null) {
            
        } else {
            $fournisseurs = $this->db
                    ->select('*')
                    ->from('SOCIETE')
                    ->where_in('SOCIETE_CLIENT', array(null, '0'))
                    ->get()
                    ->result()
            ;
            
            $data['fournisseurs'] = $fournisseurs;

            echo $this->load->view('matiere_premiere/fournir', $data, true);
        }
    }

}